package com.qifenqian.turbot.module.adapter.marketing

import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.marketing.FreeTimeDiscountDetailEntity
import kotlinx.android.synthetic.main.item_free_time_discount_edit_time.view.*

/**
 * @author H
 * @createTime 2019/1/4 10:23
 * @description
 * @see
 * @since
 */
const val TYPE_FREE_DISCOUNT_EDIT_DATE = 1
const val TYPE_FREE_DISCOUNT_EDIT_TIME = 2

//这个data中添加了"+",所以比获取的数据多一个 这个type则是划分是添加日期,还是添加时间
class FreeDiscountEditAdapter<T>(data: MutableList<T>) :
        BaseQuickAdapter<T, BaseViewHolder>(R.layout.item_free_time_discount_edit_time, data) {

    override fun convert(helper: BaseViewHolder, item: T) {
        val view = helper.itemView
        if (item is FreeTimeDiscountDetailEntity.PeriodOfLeisureTime.Date) {
            view.tv_item_free_time_discount_edit_time.text = item.discountDate
        } else if (item is FreeTimeDiscountDetailEntity.PeriodOfLeisureTime.Time) {
            if (item.discount != -1.0) {
                view.tv_item_free_time_discount_edit_time.setSpannableText("${item.startTime}-${item.endTime} ${item.discount}折", "${item.discount}折")
            } else {
                view.tv_item_free_time_discount_edit_time.text = "+"
            }
        }
        view.tv_item_free_time_discount_edit_time.setOnLongClickListener {
            if (helper.layoutPosition != data.size - 1) {
                remove(helper.layoutPosition)
            }
            return@setOnLongClickListener true
        }
        if (helper.layoutPosition == data.size - 1 && view.tv_item_free_time_discount_edit_time.text.toString() == "+") {
            view.tv_item_free_time_discount_edit_time.setOnClickListener {
                mOnAddTimeClickListener?.onClick(helper.layoutPosition)
            }
        }
    }

    fun setOnAddTimeClickListener(onAddTimeClickListener: OnAddTimeClickListener) {
        mOnAddTimeClickListener = onAddTimeClickListener
    }

    private var mOnAddTimeClickListener: OnAddTimeClickListener? = null

    interface OnAddTimeClickListener {
        fun onClick(position: Int)
    }

    private fun TextView.setSpannableText(s: String, lightText: String) {
        val spannableString = SpannableString(s)
        spannableString.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = resources.getColor(R.color.yellow_ff9100)
                ds.isUnderlineText = false
            }
        }, s.indexOf(lightText), s.indexOf(lightText) + lightText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        movementMethod = LinkMovementMethod.getInstance()
        highlightColor = resources.getColor(R.color.transparent)
        text = spannableString
    }
}